package com.leantech.android.browser.ui.search.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.leantech.android.browser.databinding.ItemSearchClipboardBinding
import com.leantech.android.browser.utils.onThrottleClick

class SearchClipboardAdapter(private val callback: SearchClipboardCallback) :
    RecyclerView.Adapter<SearchClipboardAdapter.SearchClipboardViewHolder>(
    ) {

    private var clipboardContent: CharSequence? = null

    fun updateContent(content: CharSequence?) {
        if (clipboardContent == content) {
            return
        }
        clipboardContent = content
        notifyItemChanged(0)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchClipboardViewHolder {
        val binding =
            ItemSearchClipboardBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return SearchClipboardViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return 1
    }

    override fun onBindViewHolder(holder: SearchClipboardViewHolder, position: Int) {
        holder.bind(clipboardContent, callback)
    }


    class SearchClipboardViewHolder(private val binding: ItemSearchClipboardBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(
            clipContent: CharSequence?, callback: SearchClipboardCallback
        ) {
            if (clipContent.isNullOrEmpty()) return
            binding.tvSearchLinkAddress.text = clipContent
            binding.root.onThrottleClick {
                callback.onClipItemClicked(clipContent)
            }
        }
    }


    interface SearchClipboardCallback {
        fun onClipItemClicked(clipContent: CharSequence?)

    }
}